package com.face.console.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.face.console.dao.AdminMenuMapper;
import com.face.console.pojo.vo.HomeInfoVO;
import com.face.console.pojo.vo.InitAdminVO;
import com.face.console.pojo.vo.LogoInfoVO;
import com.face.console.pojo.vo.MenuVO;
import com.face.console.service.AdminMenuService;
import com.face.console.tools.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminMenuServiceIpml implements AdminMenuService {

    @Autowired
    private AdminMenuMapper adminMenuMapper;
    @Autowired
    private HomeInfoVO homeInfo;
    @Autowired
    private LogoInfoVO logoInfo;

    @Override
    public List<MenuVO> queryMenuList() {
        return adminMenuMapper.queryAllMenus();
    }

    @Override
    public InitAdminVO querypidMenuById(Long id) {
        List<MenuVO> nodeList = listAllMenu(id);
        InitAdminVO initAdminVO = new InitAdminVO();
        initAdminVO.setHomeInfo(homeInfo);
        initAdminVO.setLogoInfo(logoInfo);
        initAdminVO.setMenuInfo(nodeList);
        return initAdminVO;
    }

    @Override
    public MenuVO queryMenuById(Long id) {
        if (id == null) {
            throw new RuntimeException("亲，ID为空了！");
        }
        return adminMenuMapper.queryMenuById(id);
    }

    @Override
    public R delById(Long id) {

        //查询该id下存在数据没
        List<MenuVO> nums = adminMenuMapper.queryMenuByPId(id);
        if (CollectionUtil.isNotEmpty(nums)) {
            for (MenuVO num : nums) {
                adminMenuMapper.deleteByPrimaryKey(num.getId());
            }
            int digital = adminMenuMapper.deleteByPrimaryKey(id);
            if (digital > 0) {
                return R.success();
            }
        } else {
            int digital = adminMenuMapper.deleteByPrimaryKey(id);
            if (digital > 0) {
                return R.success();
            }
        }
        return R.error();
    }


    public List<MenuVO> listAllMenu(Long id) {
        List<MenuVO> node = adminMenuMapper.queryMenuList(id);
        for (MenuVO dto : node) {
            // 判断是不是菜单
            if (dto.getPid().equals(id)) {
                //递归调用自己
                System.out.println("菜单名=" + dto.getTitle());
                dto.setChild(listAllMenu(dto.getId()));
            } else {
                System.out.println("节点名=");
            }
        }
        return node;
    }
}
